﻿using System.Linq.Expressions;

namespace System.Linq
{
    /// <summary>
    /// Queryable扩展方法
    /// </summary>
    public static class IQueryableExtensions
    {
        /// <summary>
        /// 满足条件时加入IQueryable查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="query"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> And<TEntity>(this IQueryable<TEntity> query, Expression<Func<TEntity, bool>> func)
        {
            return query.Where(func);
        }

        /// <summary>
        /// 满足条件时加入IQueryable查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="query"></param>
        /// <param name="condition"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> And<TEntity>(this IQueryable<TEntity> query, bool condition, Expression<Func<TEntity, bool>> func)
        {
            if (!condition)
            {
                return query;
            }
            return query.Where(func);
        }

        public static IQueryable<TResult> InnerJoin<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IQueryable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
        {
            return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector);
        }
    }
}